package com.df.tv.launcher.view;

import java.text.SimpleDateFormat;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.df.tv.launcher.R;

import com.telecom.tv189.silentlogin.UserInfo;
import com.telecom.tv189.silentlogin.UserInfoKeeper;

/**
 * 首页资讯View
 * 
 * @author John
 */
public class HomeInformationView extends FrameLayout {

    private LayoutInflater inflater;
    private TextView information_name;
    private TextView information_midday;
    private TextView information_date;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case 1:
                String stra = getdate();
                midday(stra);
                information_date.setText(stra);
                //获得当前时间
                long now = System.currentTimeMillis();
                long delay = 60000 - now % 60000;
                sendEmptyMessageDelayed(1, delay);
                break;
            default:
                break;
            }
        }
    };

    public HomeInformationView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
        getmiddaydate();
    }

    public HomeInformationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        getmiddaydate();
    }

    public HomeInformationView(Context context) {
        super(context);
        initView(context);
        getmiddaydate();
    }

    public void initView(Context context) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout view = (LinearLayout) inflater.inflate(R.layout.information_view, null);
        information_name = (TextView) view.findViewById(R.id.information_name);
        information_midday = (TextView) view.findViewById(R.id.information_midday);
        information_date = (TextView) view.findViewById(R.id.information_date);
        //首页加载的时候，给个时间
        String getdate = getdate();
        information_date.setText(getdate);
        //得到时间后 判断
        midday(getdate);

        UserInfo info = UserInfoKeeper.getInstance().getInfo();
        if (info != null) {
            String phone_name = info.getPhone();
            if (!TextUtils.isEmpty(phone_name)) {
                information_name.setText(phone_name);
            }
        }
        addView(view);
    }

    // 得到系统时间
    public String getdate() {
        long millis = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String text = sdf.format(millis);
        return text;
    }

    // 刷新时间
    public void getmiddaydate() {
        mHandler.sendEmptyMessage(1);
    }

    // 判断是上午还是下午、晚上
    public void midday(String stra) {
        String j = stra.substring(11, 13);
        int i = Integer.parseInt(j);
        if (i >= 3 && i < 8) {//早上好
            information_midday.setText(getContext().getResources().getString(R.string.information_morning));
        }
        else if (i >= 8 && i < 11) {//上午好
            information_midday.setText(getContext().getResources().getString(R.string.information_forenoon));
        }
        else if (i >= 11 && i < 14) {//中午好
            information_midday.setText(getContext().getResources().getString(R.string.information_noon));
        }
        else if (i >= 14 && i < 17) {//下午好
            information_midday.setText(getContext().getResources().getString(R.string.information_eafternoon));
        }
        else {//晚上好
            information_midday.setText(getContext().getResources().getString(R.string.information_evening));
        }
    }
}
